//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NopSolutions.NopCommerce.BusinessLogic.Regions;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class RegionLinksControl: BaseNopFrontendUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }

        protected void BindData()
        {
            var regions = this.RegionService.GetAllRegions();            

            if (regions.Count > 0)
            {
                int qregionid = CommonHelper.QueryStringInt("RegionId",0);
                int defaultRegionId = this.SettingManager.GetSettingValueInteger("Custom.DefaultRegionId");
                if ( qregionid == 0)
                {
                    if (SessionRegionId == 0)
                    {
                        SessionRegionId = defaultRegionId;
                    }
                    else
                    {
                        if (CommonHelper.GetThisPageUrl(false).Equals(CommonHelper.GetStoreLocation(), StringComparison.OrdinalIgnoreCase) || 
                            CommonHelper.GetThisPageUrl(false).Equals(CommonHelper.GetStoreLocation() + "default.aspx", StringComparison.OrdinalIgnoreCase))
                        {
                            SessionRegionId = defaultRegionId;
                        }
                    }
                }
                else
                {
                    SessionRegionId = qregionid;
                    if (qregionid == defaultRegionId)
                    {
                        var defaultregion = regions.Find(r => r.RegionID == defaultRegionId);
                        CurrentRegion = defaultregion;
                        Response.Redirect(CommonHelper.GetStoreLocation());
                    }
                }
                var curregion = regions.Find(r => r.RegionID == SessionRegionId);
                CurrentRegion = curregion;
                lbCurrentRegion.Text = Server.HtmlEncode(curregion.LocalizedName);
                regions.Remove(curregion);
                rptrRegions.DataSource = regions;
                rptrRegions.DataBind();
            }
            else
                this.Visible = false;
        }

        protected void rptrRegions_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var region = e.Item.DataItem as Region;
                var hlRegion = e.Item.FindControl("hlRegion") as HyperLink;
                if (hlRegion != null)
                {
                    hlRegion.NavigateUrl = SEOHelper.GetRegionUrl(region);
                }
            }
        }

        public int RegionId
        {
            get
            {
                return CommonHelper.QueryStringInt("RegionId",  SessionRegionId);
            }
        }
        public int SessionRegionId
        {
            get
            {
                return Session["RegionId"] == null ? 0 : int.Parse(Session["RegionId"].ToString());
            }
            set
            {
                Session["RegionId"] = value;
            }

        }
    }
}